package com.chinags.system.report.service.impl;

import java.util.List;

import com.chinags.common.exception.BusinessException;
import com.chinags.common.utils.StringUtils;
import com.chinags.system.report.domain.TJcJcbg;
import com.chinags.system.report.domain.TJcJcbgYw;
import com.chinags.system.report.mapper.TJcJcbgYwMapper;
import com.chinags.system.report.service.ITJcJcbgYwService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.chinags.common.core.text.Convert;

/**
 * 检测报告报告原文 服务层实现
 * 
 * @author liut
 * @date 2019-06-20
 */
@Service
public class TJcJcbgYwServiceImpl implements ITJcJcbgYwService
{
	private static final Logger log = LoggerFactory.getLogger(TJcJcbgYwServiceImpl.class);
	@Autowired
	private TJcJcbgYwMapper tJcJcbgYwMapper;

	/**
     * 查询检测报告报告原文信息
     * 
     * @param textId 检测报告报告原文ID
     * @return 检测报告报告原文信息
     */
    @Override
	public TJcJcbgYw selectTJcJcbgYwById(Long textId)
	{
	    return tJcJcbgYwMapper.selectTJcJcbgYwById(textId);
	}
	
	/**
     * 查询检测报告报告原文列表
     * 
     * @param tJcJcbgYw 检测报告报告原文信息
     * @return 检测报告报告原文集合
     */
	@Override
	public List<TJcJcbgYw> selectTJcJcbgYwList(TJcJcbgYw tJcJcbgYw)
	{
	    return tJcJcbgYwMapper.selectTJcJcbgYwList(tJcJcbgYw);
	}
	
    /**
     * 新增检测报告报告原文
     * 
     * @param tJcJcbgYw 检测报告报告原文信息
     * @return 结果
     */
	@Override
	public int insertTJcJcbgYw(TJcJcbgYw tJcJcbgYw)
	{
	    return tJcJcbgYwMapper.insertTJcJcbgYw(tJcJcbgYw);
	}
	
	/**
     * 修改检测报告报告原文
     * 
     * @param tJcJcbgYw 检测报告报告原文信息
     * @return 结果
     */
	@Override
	public int updateTJcJcbgYw(TJcJcbgYw tJcJcbgYw)
	{
	    return tJcJcbgYwMapper.updateTJcJcbgYw(tJcJcbgYw);
	}

	/**
     * 删除检测报告报告原文对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
	@Override
	public int deleteTJcJcbgYwByIds(String ids)
	{
		return tJcJcbgYwMapper.deleteTJcJcbgYwByIds(Convert.toStrArray(ids));
	}

	@Override
	public String importUser(List<TJcJcbgYw> tJcJcbgYwList, boolean updateSupport, String operName) {
		if (StringUtils.isNull(tJcJcbgYwList) || tJcJcbgYwList.size() == 0)
		{
			throw new BusinessException("导入用户数据不能为空！");

		}

		int successNum = 0;
		int failureNum = 0;
		StringBuilder successMsg = new StringBuilder();
		StringBuilder failureMsg = new StringBuilder();
//		String password = configService.selectConfigByKey("sys.tjcFlzddw.initPassword");
		for (TJcJcbgYw tJcJcbgYw : tJcJcbgYwList)
		{
			try
			{
				// 验证是否存在这个报告
				TJcJcbgYw t = tJcJcbgYwMapper.selectTjcjcbgYwBytextId(tJcJcbgYw.getTextId());
				if (StringUtils.isNull(t))
				{
					tJcJcbgYw.setCreateBy(operName);
					this.insertTJcJcbgYw(tJcJcbgYw);
					successNum++;
					successMsg.append("<br/>" + successNum + "、 报告" + tJcJcbgYw.getTextName() + " 导入成功");
				}
				else if (updateSupport)
				{
					tJcJcbgYw.setUpdateBy(operName);
					this.updateTJcJcbgYw(tJcJcbgYw);
					successNum++;
					successMsg.append("<br/>" + successNum + "、 " + tJcJcbgYw.getTextName() + " 更新成功");
				}
				else
				{
					failureNum++;
					failureMsg.append("<br/>" + failureNum + "、单位 " + tJcJcbgYw.getTextName() + " 已存在");
				}
			}
			catch (Exception e)
			{
				failureNum++;
				String msg = "<br/>" + failureNum + "、单位 " + tJcJcbgYw.getTextName() + " 导入失败：";
				failureMsg.append(msg + e.getMessage());
				log.error(msg, e);
			}
		}
		if (failureNum > 0)
		{
			failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
			throw new BusinessException(failureMsg.toString());
		}
		else
		{
			successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
		}
		return successMsg.toString();

	}

	@Override
	public TJcJcbgYw selectJobLogById(Long textMessageId) {
		return tJcJcbgYwMapper.selectJobLogById(textMessageId);
	}


}
